package com.xmg.mgrsite.base;

import com.alibaba.fastjson.JSON;
import com.xmg.p2p.base.domain.UserFile;
import com.xmg.p2p.base.domain.Userinfo;
import com.xmg.p2p.base.query.UserFileQueryObject;
import com.xmg.p2p.base.service.RealAuthService;
import com.xmg.p2p.base.service.UserFileService;
import com.xmg.p2p.base.service.UserinfoService;
import com.xmg.p2p.base.util.BidConst;
import com.xmg.p2p.base.util.JSONResult;
import com.xmg.p2p.business.domain.BidRequest;
import com.xmg.p2p.business.query.BidRequestQueryObject;
import com.xmg.p2p.business.service.IBidRequestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class BidRequestController {
    @Autowired
    private IBidRequestService bidRequestService ;
    @Autowired
    private UserinfoService userinfoService;
    @Autowired
    private RealAuthService realAuthService;
    @Autowired
    private UserFileService userFileService;

    //进入发标前审核页面
    @RequestMapping("bidrequest_publishaudit_list")
    public String publishAuditList(@ModelAttribute("qo") BidRequestQueryObject qo , Model model ){
        //设置借款对象状态为待发布状态
        qo.setBidRequestState(BidConst.BIDREQUEST_STATE_PUBLISH_PENDING) ;
        //获取分页对象
        model.addAttribute("pageResult" ,this.bidRequestService.query(qo)) ;
        return "bidrequest/publish_audit" ;
    }


    /**
     * 发标之前的审核
     */
    @RequestMapping("bidrequest_publishaudit")
    @ResponseBody
    public JSONResult publishAudit(Long id, String remark, int state) {
        JSONResult json = new JSONResult();
        try {
            this.bidRequestService.publishAudit(id, remark, state);
        } catch (Exception e) {
            e.printStackTrace();
            json.setSuccess(false);
            json.setMsg(e.getMessage());
        }
        return json;
    }

    /**
     * 查看用户信息
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("borrow_info")
    public String borrowInfoDetail(Long id,Model model){
        //根据借款对象id获取借款对象
        BidRequest bidRequest = this.bidRequestService.getById(id);
        //获得当前的借款人
        Userinfo userinfo = this.userinfoService.getById(bidRequest.getCreateUser().getId());
        model.addAttribute("bidRequest",bidRequest);
        model.addAttribute("userInfo",userinfo);
        //查询当前标的 历史审核对象
        model.addAttribute("audits", this.bidRequestService.listAuditHistorayByBidRequest(bidRequest.getId()));
        //用户的实名认证信息
        model.addAttribute("realAuth", this.realAuthService.get(userinfo.getRealAuthId()));

        //借款人所有的相关的风控材料(已经审核通过)
        UserFileQueryObject qo = new UserFileQueryObject() ;
        qo.setState(UserFile.STATE_AUDIT);
        qo.setApplierId(userinfo.getId());
        qo.setPageSize(-1); //为了检索从某一个偏移量到记录集的结束所有的记录行，可以指定第二个参数为 -1：
        model.addAttribute("userFiles", this.userFileService.queryForList(qo));
        return "bidrequest/borrow_info";
    }

    /**
     * 打开满标一审
     * @param qo    审核
     * @param model 模板
     * @return  返回显示页面
     */
    @RequestMapping("bidrequest_audit1_list.do")
    public String audit1List(@ModelAttribute("qo") BidRequestQueryObject qo, Model model){
        qo.setBidRequestState(BidConst.BIDREQUEST_STATE_APPROVE_PENDING_1);
        model.addAttribute("pageResult",this.bidRequestService.query(qo));
        return "bidrequest/audit1";
    }

    /**
     * 执行一审的审核
     * @param id    要审核的标
     * @param remark    审核备注
     * @param state 状态
     * @return  返回JSON封装执行结果的信息
     */
    @ResponseBody
    @RequestMapping("/bidrequest_audit1.do")
    public JSONResult fullAudit1(Long id,String remark, int state){
        JSONResult jsonResult = new JSONResult();
        try {
            this.bidRequestService.fullAudit1(id,remark,state);
        }catch (Exception ex){
            ex.printStackTrace();
            jsonResult.setSuccess(false);
            jsonResult.setMsg(ex.getMessage());
        }
        return jsonResult;
    }

    /**
     * 进入二审页面
     * @param qo    条件
     * @param model 模型
     * @return  要返回到的页面
     */
    @RequestMapping("/bidrequest_audit2_list.do")
    public String audit2List( @ModelAttribute("qo") BidRequestQueryObject qo ,Model model){
        qo.setBidRequestState(BidConst.BIDREQUEST_STATE_APPROVE_PENDING_2); // 条件设置要查询的审核状态为二审状态
        model.addAttribute("pageResult",this.bidRequestService.query(qo));  // 根据条件查询
        return "bidrequest/audit2";
    }

    /**
     * 进行二审
     * @param id   标的ID
     * @param remark    二审的备注信息
     * @param state     审核的状态
     * @return  返回执行的结果JSON
     */
    @ResponseBody
    @RequestMapping("bidrequest_audit2")
    public JSONResult fullAudit2(Long id, String remark, int state) {
        JSONResult json = new JSONResult();
        try {
            this.bidRequestService.fullAudit2(id, remark, state);
        } catch (Exception ex) {
            ex.printStackTrace();
            json.setSuccess(false);
            json.setMsg(ex.getMessage());
        }
        return json;
    }
}
